1 /********************************** Module Header **********************************\
2 * Module Name: SimulateVal.cs
3 * Project: CSDynamicallyBuildLambdaExpressionWithField
4 * Copyright (c) Microsoft Corporation.
6 * The SimulateVal.cs file defines variant function which is deal with DateTime and Boolean.
8 * This source is subject to the Microsoft Public License.
9 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 * All other rights reserved.
12 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
13 * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
15 \***********************************************************************************/
17 public static class SimulateVal
19 public static double Val(string expression
)
21 if (expression
== null)
25 // Try the entire string, then progressively smaller
26 // substrings to simulate the behavior of VB's 'Val',
27 // which ignores trailing characters after a recognizable value:
28 for (int size
= expression
.Length
; size
> 0; size
--)
31 if (double.TryParse(expression
.Substring(0, size
), out testDouble
))
35 // No value is recognized, so return 0:
39 public static double Val(object expression
)
41 if (expression
== null)
47 if (double.TryParse(expression
.ToString(), out testDouble
))
51 // CSharp's 'Val' function returns -1 for 'true':
54 if (bool.TryParse(expression
.ToString(), out testBool
))
56 return testBool
? -1 : 0;
58 // CSharp's 'Val' function returns the day of the month for dates:
59 System
.DateTime testDate
;
60 if (System
.DateTime
.TryParse(expression
.ToString(), out testDate
))
64 // No value is recognized, so return 0:
70 /// Convert char into string
72 public static int Val(char expression
)
75 if (int.TryParse(expression
.ToString(), out testInt
))